package com.hsy.crm.web.service.impl;

import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.PageResult;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.Role;
import com.hsy.crm.web.domain.entity.RoleMenu;
import com.hsy.crm.web.domain.entity.UserRole;
import com.hsy.crm.web.domain.ex.ServiceException;
import com.hsy.crm.web.domain.po.role.RoleListPo;
import com.hsy.crm.web.domain.po.role.RolePagePo;
import com.hsy.crm.web.domain.vo.role.RoleAuthorityVo;
import com.hsy.crm.web.domain.vo.role.RoleListVo;
import com.hsy.crm.web.domain.vo.role.RolePageVo;
import com.hsy.crm.web.mapper.RoleMapper;
import com.hsy.crm.web.mapper.RoleMenuMapper;
import com.hsy.crm.web.mapper.UserRoleMapper;
import com.hsy.crm.web.service.base.BaseService;
import com.hsy.crm.web.utils.ListUtils;
import com.hsy.crm.web.utils.UuidUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleService extends BaseService<Role> {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    RoleMenuMapper roleMenuMapper;

    @Autowired
    UserRoleMapper userRoleMapper;

    public Unified<Integer> deleteRole(Role role) {
        Unified<Integer> unified = Unified.create(Integer.class);
        if(hasMenu(role)){
            unified.setResult(App.BussError.ROLE_DELETE_MENU.getCode());
        }else if(hasUser(role)){
            unified.setResult(App.BussError.ROLE_DELETE_USER.getCode());
        }else{
            this.delete(role);
        }
        return unified;
    }

    public PageResult<RolePagePo> page(RolePageVo rolePageVo) {
        Page page = PageHelper.startPage(rolePageVo.getPage(),rolePageVo.getLimit());
        PageResult<RolePagePo> pageResult = new PageResult<>();
        pageResult.setRows(roleMapper.page(rolePageVo));
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    public PageResult<RolePagePo> userRolePage(RolePageVo rolePageVo) {
        Page page = PageHelper.startPage(rolePageVo.getPage(),rolePageVo.getLimit());
        PageResult<RolePagePo> pageResult = new PageResult<>();

        if(rolePageVo.getCompanyId()==null){
            rolePageVo.setCompanyId(rolePageVo.getLoginCompanyId());
        }

        pageResult.setRows(roleMapper.userRolePage(rolePageVo));
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    @Transactional
    public Unified<Integer> authority(RoleAuthorityVo authorityVo){
        Unified<Integer> unified = Unified.create(Integer.class);

        //登陆人不能修改自己的角色权限
        UserRole userRole = new UserRole();
        userRole.setCustomerId(authorityVo.getCustomerId());
        userRole.setCompanyId(authorityVo.getLoginCompanyId());
        userRole.setUserId(authorityVo.getLoginUserId());
        List<UserRole> userRoles = userRoleMapper.select(userRole);
        for (UserRole ur : userRoles){
            if( ur.getRoleId().equals(authorityVo.getRoleId()) ){
                throw new ServiceException(App.BussError.ROLE_AUT.getCode());
            }
        }

        RoleMenu delRoleMenu = new RoleMenu();
        delRoleMenu.setRoleId(authorityVo.getRoleId());
        delRoleMenu.setCustomerId(authorityVo.getCustomerId());
        roleMenuMapper.delete(delRoleMenu);

        if( ListUtils.isNotEmpty(authorityVo.getMenuIds()) ){
            for (Long menuId : authorityVo.getMenuIds()) {
                Long id = UuidUtils.getUuid();
                RoleMenu roleMenu = new RoleMenu(id,authorityVo.getRoleId(),menuId,authorityVo.getCustomerId());
                roleMenuMapper.insert(roleMenu);
            }
        }
        return unified;
    }

    public Unified<List<RoleListPo>> findMenus(RoleListVo roleListVo){
        Unified<List<RoleListPo>> unified = new Unified<>();
        unified.setData(roleMapper.findMenus(roleListVo));
        return unified;
    }

    private boolean hasUser(Role role){
        RoleMenu roleMenu = getRoleMenu(role);
        Long userId = roleMapper.hasUser(roleMenu);
        return userId != null ;
    }

    private boolean hasMenu(Role role){
        RoleMenu roleMenu = getRoleMenu(role);
        Long menuId = roleMapper.hasMenu(roleMenu);
        return menuId != null ;
    }

    private RoleMenu getRoleMenu(Role role){
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(role.getId());
        roleMenu.setCustomerId(role.getCustomerId());
        return roleMenu;
    }

}
